#ifndef MQTT_DEV_H
#define MQTT_DEV_H

#include <QObject>
extern "C" {
    #include "MQTTClient.h"
}

#define ONENET_ADDRESS "a1fhO3qSWHg.iot-as-mqtt.cn-shanghai.aliyuncs.com"   //阿里云云平台工业物联网产品MQTT服务器地址

extern QByteArray DATA_DEV_CLIENT_ID;
extern QByteArray DATA_DEV_USERNAME;
extern QByteArray DATA_DEV_PASSWORD;

#define DATA_DEV_TOPIC  QString::asprintf("/a1fhO3qSWHg/%s/user/update",mqtt_dev::get_dev_name(DATA_DEV_USERNAME.constData())).toLatin1().data()
#define DATA_DEV_PUB_TOPIC  QString::asprintf("/sys/a1fhO3qSWHg/%s/thing/event/property/post",mqtt_dev::get_dev_name(DATA_DEV_USERNAME.constData())).toLatin1().data()
#define DATA_DEV_PUB_DATA(IDENT,VAL) QString::asprintf("{\"method\":\"thing.event.property.post\",\"id\":\"0000000001\",\"params\":{\"%s\":%.1f},\"version\":\"1.0.0\"}",IDENT,VAL).toLatin1().data()

#define OFF_LINE_DATA_CLIENT_ID "off-line_data|securemode=3,signmethod=hmacsha1|"
#define OFF_LINE_DATA_USERNAME  "off-line_data&a1fhO3qSWHg"
#define OFF_LINE_DATA_PASSWORD  "13354a3475d828cf5b2f4b5c17d177fd884bfc02"

#define OFF_LINE_DATA_TOPIC "/a1fhO3qSWHg/off-line_data/user/update"
#define OFF_LINE_DATA_PUB_TOPIC "/sys/a1fhO3qSWHg/off-line_data/thing/event/property/post"
#define OFF_LINE_DATA_PUB_DATA(IDENT,VAL) QString::asprintf("{\"method\":\"thing.event.property.post\",\"id\":\"0000000001\",\"params\":{\"%s\":%.1f},\"version\":\"1.0.0\"}",IDENT,VAL).toLatin1().data()

class mqtt_dev : public QObject
{
    Q_OBJECT
private:
    MQTTClient client;

public:
    explicit mqtt_dev(QObject *parent = nullptr);
    explicit mqtt_dev(const char *serverURL,const char *clientid,int pers_type,void *pers_context);
    virtual ~mqtt_dev();

    int connect(MQTTClient_connectOptions *options);
    void disconnect();
    int setCallback(void *context,MQTTClient_connectionLost *cl,
                    MQTTClient_messageArrived *ma,
                    MQTTClient_deliveryComplete *dc);
    MQTTClient* get_client(void);

    static const char *get_dev_name(const char *userName);  //从用户名中获取到设备名

signals:

};

QString uname_get_dname(const char *userName);

#endif // MQTT_DEV_H
